package com.xgimi.open.skill.datamanager.domain.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xgimi.open.skill.datamanager.domain.entity.SkillDO;
import com.xgimi.open.skill.datamanager.domain.mapper.SkillMapper;
import com.xgimi.open.skill.datamanager.domain.service.SkillService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Objects;

/**
 * @author coderzpw.zhang
 * @description 针对表【skill(技能表)】的数据库操作Service实现
 * @createDate 2023-02-07 19:30:37
 */
@Service
public class SkillServiceImpl extends ServiceImpl<SkillMapper, SkillDO>
        implements SkillService {

    public static final Integer PUBLISHED = 1;

    @Override
    public List<SkillDO> querySkillByName(SkillDO skillDO) {
        LambdaQueryWrapper<SkillDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(!StringUtils.isEmpty(skillDO.getSkillName()), SkillDO::getSkillName, skillDO.getSkillName());
        List<SkillDO> list = this.list(queryWrapper);
        return list;
    }

    @Override
    public Boolean saveSkill(SkillDO skillDO) {
        return this.save(skillDO);
    }

    @Override
    public IPage<SkillDO> pageQuery(IPage<SkillDO> page, SkillDO mapperReq) {
        LambdaQueryWrapper<SkillDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(!Objects.isNull(mapperReq.getId()), SkillDO::getId, mapperReq.getId());
        queryWrapper.eq(!StringUtils.isEmpty(mapperReq.getSkillName()), SkillDO::getSkillName, mapperReq.getSkillName());
        queryWrapper.eq(!StringUtils.isEmpty(mapperReq.getCreatedBy()), SkillDO::getCreatedBy, mapperReq.getCreatedBy());
        queryWrapper.orderByDesc(SkillDO::getTop);
        queryWrapper.orderByDesc(SkillDO::getTopTime);
        queryWrapper.orderByDesc(SkillDO::getCreatedAt);
        return this.page(page, queryWrapper);
    }

    @Override
    public SkillDO queryById(Long id) {
        return this.getById(id);
    }

    /**
     * 不是这个id，但是是这个名字
     *
     * @param skillDO
     * @return
     */
    @Override
    public List<SkillDO> queryByNameAndNoId(SkillDO skillDO) {
        LambdaQueryWrapper<SkillDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SkillDO::getSkillName, skillDO.getSkillName());
        queryWrapper.ne(SkillDO::getId, skillDO.getId());
        return this.list(queryWrapper);
    }

    @Override
    public boolean updateByIdAndVersion(SkillDO skillDO) {
        LambdaUpdateWrapper<SkillDO> updateWrapper = new LambdaUpdateWrapper<>();
        Integer updateVersion = skillDO.getUpdateVersion();
        updateWrapper.eq(SkillDO::getUpdateVersion, updateVersion);
        updateWrapper.eq(SkillDO::getId, skillDO.getId());
        // 版本自增
        skillDO.setUpdateVersion(updateVersion + 1);
        return this.update(skillDO, updateWrapper);
    }

    @Override
    public List<SkillDO> getPublishedSkill() {
        LambdaQueryWrapper<SkillDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SkillDO::getPublished, PUBLISHED);
        return this.list(queryWrapper);
    }

    @Override
    public SkillDO queryByBusinessId(String skillBusinessId) {
        LambdaQueryWrapper<SkillDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SkillDO::getBusinessId, skillBusinessId);
        return this.getOne(queryWrapper);
    }

    @Override
    public List<SkillDO> onlineSkills() {
        LambdaQueryWrapper<SkillDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SkillDO::getPublished, PUBLISHED);
        return this.list(queryWrapper);
    }

    @Override
    public List<SkillDO> testSkills() {
        return this.list();
    }
}




